/**
 * 
 */
package vn.smartware.co.framework.utils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import vn.smartware.co.framework.exception.ClientException;
import vn.smartware.co.framework.validator.ActionError;
import vn.smartware.co.framework.validator.Resources;


/**
 * @author thuctt
 *
 */
public class MsgBoxUtils {

	/**
	 * create info messagebox
	 * @param code
	 * @return int
	 * @throws ClientException 
	 */
	public static int infoMsgBox(String code)
			throws ClientException {
		
		return infoMsgBox(code, new Object[]{});
	}
	
	/**
	 * create info messagebox
	 * @param code
	 * @param message
	 * @return int
	 * @throws ClientException 
	 */
	public static int infoMsgBox(String code, Object[] message)
			throws ClientException {
		
		Display display = Display.getCurrent();
		Shell shell = display.getActiveShell();
		if (null == shell){
			throw new ClientException();
		}
		String msg = getMessage(code, message);
		int style = 0 | SWT.ICON_INFORMATION | SWT.OK;
		MessageBox mb = new MessageBox(shell, style);
		mb.setText("Information");
		mb.setMessage(msg);
		return mb.open();
	}
	
	/**
	 * create error messagebox
	 * @param errCode
	 * @return int
	 * @throws ClientException 
	 */
	public static int errorMsgBox(String errCode)
			throws ClientException {
		
		return errorMsgBox(errCode, new Object[]{});
	}
	
	/**
	 * create error messagebox
	 * @param errCode
	 * @param message
	 * @return int
	 * @throws ClientException 
	 */
	public static int errorMsgBox(String errCode, Object[] message)
			throws ClientException {
		
		Display display = Display.getCurrent();
		Shell shell = display.getActiveShell();
		if (null == shell){
			throw new ClientException();
		}
		String msg = getMessage(errCode, message);
		int style = 0 | SWT.ICON_ERROR | SWT.OK;
		MessageBox mb = new MessageBox(shell, style);
		mb.setText("Error");
		mb.setMessage(msg);
		return mb.open();
	}
	
	/**
	 * create warning messagebox
	 * @param errCode
	 * @return int
	 * @throws ClientException 
	 */
	public static int warningMsgBox(String errCode)
			throws ClientException {
		
		return warningMsgBox(errCode, new Object[]{});
	}
	
	/**
	 * create warning messagebox
	 * @param errCode
	 * @param message
	 * @return int
	 * @throws ClientException 
	 */
	public static int warningMsgBox(String errCode, Object[] message)
			throws ClientException {
		
		Display display = Display.getCurrent();
		Shell shell = display.getActiveShell();
		if (null == shell){
			throw new ClientException();
		}
		String msg = getMessage(errCode, message);
		int style = 0 | SWT.ICON_WARNING | SWT.OK;
		MessageBox mb = new MessageBox(shell, style);
		mb.setText("Warning");
		mb.setMessage(msg);
		return mb.open();
	}
	
	/**
	 * create confirm messagebox
	 * @param errCode
	 * @return int
	 * @throws ClientException 
	 */
	public static int confirmMsgBox(String errCode)
			throws ClientException {
		
		return confirmMsgBox(errCode, new Object[]{});
	}
	
	/**
	 * create confirm messagebox
	 * @param errCode
	 * @param message
	 * @return int
	 * @throws ClientException 
	 */
	public static int confirmMsgBox(String errCode, Object[] message)
			throws ClientException {
		
		Display display = Display.getCurrent();
		Shell shell = display.getActiveShell();
		if (null == shell){
			throw new ClientException();
		}
		String msg = getMessage(errCode, message);
		int style = 0 | SWT.ICON_INFORMATION | SWT.YES | SWT.NO | SWT.CANCEL;
		MessageBox mb = new MessageBox(shell, style);
		mb.setText("Confirm");
		mb.setMessage(msg);
		return mb.open();
	}
	
	/**
	 * create confirm messagebox
	 * @param errCode
	 * @return int
	 * @throws ClientException 
	 */
	public static int questionMsgBox(String errCode)
			throws ClientException {
		
		return questionMsgBox(errCode, new Object[]{});
	}
	
	/**
	 * create confirm messagebox
	 * @param errCode
	 * @param message
	 * @return int
	 * @throws ClientException 
	 */
	public static int questionMsgBox(String errCode, Object[] message)
			throws ClientException {
		
		Display display = Display.getCurrent();
		Shell shell = display.getActiveShell();
		if (null == shell){
			throw new ClientException();
		}
		String msg = getMessage(errCode, message);
		int style = 0 | SWT.ICON_QUESTION | SWT.YES | SWT.NO;
		MessageBox mb = new MessageBox(shell, style);
		mb.setText("Question");
		mb.setMessage(msg);
		return mb.open();
	}

	/**
	 * get message from ClientApplicationResources.properties
	 * @param errCode
	 * @param message
	 * @return String
	 * @throws ClientException
	 */
	public static String getMessage(String errCode, Object[] message)
			throws ClientException {
		
		try {
			ActionError errorMsgObj = new ActionError(errCode, message);
			String errorMessage = Resources.getMessage(errorMsgObj);
			return errorMessage;
		} catch (Exception e) {
			throw new ClientException();
		}
		
	}
	
	/**
	 * create fatal error messagebox
	 * @return int
	 */
	public static int fatalErrorMsgBox() {
		
		int res = -1;
		try {
			res = errorMsgBox("errors.SYSTEM_ERROR");
			
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * set Status Label Message
	 * @param clblMsg 
	 * @param errCd 
	 */
	public static void setStatusLabelMessage(CLabel clblMsg, String errCd) {
		
		try {
			String msg = getMessage(errCd, new Object[]{});
			clblMsg.setText(msg);
		
		}catch (ClientException e) {
			e.printStackTrace();
		}
	}
}
